package com.zhanqiao.controller;

import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.ResultSetHandler;
import org.apache.commons.dbutils.handlers.ArrayHandler;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Arrays;
@WebServlet("/registeduser")
public class RegistedController extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String userName = req.getParameter("username");
        String userPassword = req.getParameter("userpassword");
        String userMail = req.getParameter("usermail");
        DataSource ds = new ComboPooledDataSource();
        QueryRunner queryRunner = new QueryRunner(ds);
        resp.setContentType("text/html;charset=utf8");
        Object[] parm = {userName, userPassword, userMail};
        try {
            Object[] insert = queryRunner.insert("insert into user(uname,password,umail) values(?,?,?)", new ArrayHandler(), parm);
            if(insert.length>0){
                req.setAttribute("message","注册成功");
                req.getRequestDispatcher("/success.html").forward(req,resp);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
